package com.li.general.buttons
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;

public class RollBackBtn extends Button
{
    public var backupSpeed:uint = 1;

    private var _rollingBack:Boolean;
    private var _enterFrameActive:Boolean;

    public function RollBackBtn()
    {
        super();
    }

    // Roll.
    private function startEnterFrame():void
    {
        if(_enterFrameActive)
            return;

        _enterFrameActive = true;
        if(!hasEventListener(Event.ENTER_FRAME))
            addEventListener(Event.ENTER_FRAME, enterFrameHandler);
    }
    private function stopEnterFrame():void
    {
        if(!_enterFrameActive)
            return;

        _enterFrameActive = false;
        if(hasEventListener(Event.ENTER_FRAME))
            removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
    }

    // Mouse events.
    override protected function hitMouseOverHandler(evt:MouseEvent):void
    {
        play();
        _rollingBack = false;
        startEnterFrame();
    }

    override protected function hitMouseOutHandler(evt:MouseEvent):void
    {
        stop();
        _rollingBack = true;
        startEnterFrame();
    }

    // EnterFrame (only active on mouse interactions with hit).
    private function enterFrameHandler(evt:Event):void
    {
        if(_rollingBack)
        {
            if(currentFrameLabel != "start")
                gotoAndStop(Math.max(1, currentFrame - backupSpeed));
            else
                stopEnterFrame();
        }
        else
        {
            if(currentFrameLabel == "end")
            {
                stop();
                stopEnterFrame();
            }
        }
    }
}
}